package cloudoer.su.interceptor;

import cloudoer.su.base.impl.BaseAction;
import cloudoer.su.entity.Menu;
import cloudoer.su.entity.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 权限拦截器
 */
@Component
public class JurisdictionInterceptor extends AbstractInterceptor {
    private static final Logger logger = Logger.getLogger(JurisdictionInterceptor.class);

    public String intercept(ActionInvocation invocation) throws Exception {
        BaseAction action = (BaseAction) invocation.getAction();
        ActionContext actionContext = ActionContext.getContext();
        User user = (User) actionContext.getSession().get("user");
        List<Menu> list = (List<Menu>) actionContext.getSession().get("userMenus");
        if (user == null || list == null || list.size() <= 0){
            ServletActionContext.getResponse().setStatus(403);
            return "error";
        }
        return invocation.invoke();
    }
}
